/*
	Copyright 2010, Manuele Turini
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	3. Neither name of the copyright holders nor the names of its contributors may
	be used to endorse or promote products derived from this software without
	specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
	FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
	DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
	CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
	OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <QIcon>

#include "qdisplay.h"

extern int qInitResources_QDisplay();

QDisplay::QDisplay(QWidget * parent, Qt::WindowFlags fl)
: QWidget(parent, fl)
{
	initAll(Huge);
	currentFormat = "#";
	setDisplayFormat(currentFormat);
}

QDisplay::QDisplay(const QString & format, DisplaySize siz, QWidget * parent, Qt::WindowFlags fl)
: QWidget(parent, fl)
{
 	initAll(siz);
 	currentFormat = format;
 	setDisplayFormat(format);
}

QDisplay::~QDisplay()
{

}

void QDisplay::initAll(DisplaySize s)
{
	qInitResources_QDisplay();

	setObjectName("QDisplay");
	setWindowIcon(QIcon(":/QDisplay/images/n08_3.png"));

	QDisplayLayout = new QHBoxLayout(this);
	QDisplayLayout->setSpacing(0);

	pathType[0] = 'r';
	pathType[1] = 'n';
	pathType[2] = 'd';
	type = Red_7_Segment;
	siz = Huge;

	for(int c = 0; c < MAX_NUMBER_OF_DIGITS; c++)
	{
		Display[c] =  new QLabel(this);
		Display[c]->setScaledContents(true);
		QDisplayLayout->addWidget(Display[c]);
		Display[c]->hide();
	}
	setLayout(QDisplayLayout);
	setDisplaySize(s);
}

void QDisplay::setDisplaySize(DisplaySize dsiz)
{
	if(dsiz >= Small && dsiz <= Huge)
	{
		QString str;
		siz = dsiz;
		// Load all pixmaps
		for(int c = 0; c < 10; c++)
		{
			str.sprintf(":/QDisplay/images/%c%02d_%d.png", pathType[type], c, dsiz);
			digits[c] = QPixmap(str);
		}
		str.sprintf(":/QDisplay/images/%cblank_%d.png", pathType[type], dsiz);
		blank = QPixmap(str);
		str.sprintf(":/QDisplay/images/%cminus_%d.png", pathType[type], dsiz);
		minus = QPixmap(str);
		str.sprintf(":/QDisplay/images/%cpoint_%d.png", pathType[type], dsiz);
		point = QPixmap(str);

		setDisplayFormat(currentFormat);
	}
}

void QDisplay::setDisplayFormat(const QString & fmt)
{
	int		c, bb = 0, pd = 0, ad = 0;
	bool	dp = false;

	//	qDebug("Format = %s, Len = %d\n", fmt.ascii(), fmt.length());

	for(c = 0; c < MAX_NUMBER_OF_DIGITS; c++)
	{
		if(c < fmt.length())
		{
			switch(fmt[c].toLatin1())
			{
			case '#': 	Display[c]->setPixmap(digits[8]);
				if(dp)
					ad++;
				else
					pd++;
				break;
			case ' ':	Display[c]->setPixmap(blank);
				bb++;
				break;
			case '.':	Display[c]->setPixmap(point);
				dp  = true;
				break;
			}
			Display[c]->show();
		}
		else
			Display[c]->hide();
	}
	printfFmt.sprintf("%%%d.%dlf", pd + bb + dp + ad, ad);
 	resize(minimumSizeHint());

	if(currentFormat != fmt)
		currentFormat = fmt.left(MAX_NUMBER_OF_DIGITS);
}

void QDisplay::display(const QString & dsp)
{
	for(int c = 0; c < MAX_NUMBER_OF_DIGITS; c++)
	{
		if(!Display[c]->isHidden())
		{
			int ch = dsp[c].toLatin1();
			switch(ch)
			{
			case '0': case '1': case '2': case '3': case '4':	
			case '5': case '6': case '7': case '8': case '9':
				Display[c]->setPixmap(digits[ch - 48]);
				break;
			case ' ':	Display[c]->setPixmap(blank);
				break;
			case '.':	Display[c]->setPixmap(point);
				break;
			case '-':	Display[c]->setPixmap(minus);
				break;
			}
		}
		else
			break;
	}
}

void QDisplay::setDisplayType(DisplayType t)
{
	if(t >= 0 && t <= 2)
	{
		type = t; 
		setDisplaySize(displaySize()); 
	}
}

void QDisplay::display(double num)
{
	QString out;

        this->m_currentValue = num; // memorize
	out.sprintf(printfFmt.toUtf8().data(), num);

	display(out);
}

double QDisplay::display(void)
{
    return (this->m_currentValue);
}

QIcon QDisplay::icon()
{
	return QIcon(":/QDisplay/images/n08_3.png");
}
